package nl.hva.web.mvcwebapp.chat;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.util.List;
import nl.hva.web.mvcwebapp.chat.*;
import nl.hva.web.mvcwebapp.doa.CourseDAO;
import nl.hva.web.mvcwebapp.models.Course;

/** Allows users to add new rooms.
* At server startup this servlet is initialised.

*/
public class ManageChatServlet extends HttpServlet
{
        public static ChatRoomList rooms = new ChatRoomList();
        Properties props = null;
        /** Reads chat.properties file creates an object of ChatRoomList and stores it in ServletContext
        */
        public void init() throws ServletException
        {
                        String path = "";
                        //path = "/WEB-INF/"+getServletContext().getInitParameter("chatpropertyfile");
                        //String realPath;
                        //realPath = getServletContext().getRealPath(path);
                        
                        /*InputStream fis = getServletContext().getResourceAsStream(path);

                        if (fis != null)
                        {*/
                        System.out.println("chat init.");
                         List<Course> courselist = CourseDAO.getAllCourses();
                        for(Course item : courselist ){
                            String roomName = item.getCoursetitle();
                            String roomDescr = item.getCoursetitle();
                            int id =item.getCourseid();
                            addNewRoom(rooms, roomName, roomDescr,id);
                            System.out.println(roomName);
                            System.out.println(roomDescr);
                        }
                        int id= 0;
                        addNewRoom(rooms, "StartUp", "StartUp",id);
                        
                        getServletContext().setAttribute("chatroomlist", rooms);
                        System.err.println("Room List Created");
//                      if (realPath != null)
//                      {
//                              InputStream fis = new FileInputStream(realPath);
//
//                              props = new Properties();
//                              props.load(fis);
//                              Enumeration keys = props.keys();
//                              while (keys.hasMoreElements())
//                              {
//                                      String roomName = (String)keys.nextElement();
//                                      String roomDescr = (String)props.getProperty(roomName);
//                                      addNewRoom(rooms, roomName, roomDescr);
//                              }
//                              fis.close();
//                              getServletContext().setAttribute("chatroomlist", rooms);
//                              System.err.println("Room List Created");
//                      }
//                      else
//                      {
//                              System.out.println("Unable to get realpath of chatproperty file " + path + ".\nCheck that application war file is expanded and file can be read.\nChat application won't work.");
//                      }
                
                
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println("Room List Created");
                out.close();
        }
        
        /**     Allows users to add new rooms after performing minimum validation.
        * Also saves information to chat.properties files if required by initialization parameter <code>saveRooms</code>.
        */
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {          
        }
        /**
        * Adds a new Room to ChatRoomList object and saves it to chat.properties file if required.
        */
        public void addNewRoom(ChatRoomList list, String roomName, String roomDescr, int id)
        {
                String s = getServletContext().getInitParameter("maxNoOfMessages");
                int maxMessages = 25;
                if (s != null)
                {
                        try
                        {
                                maxMessages = Integer.parseInt(s);
                        }
                        catch (NumberFormatException nfe)
                        {
                                
                        }
                }
                ChatRoom room = new ChatRoom(roomName, roomDescr,id);
                room.setMaximumNoOfMessages(maxMessages);
                rooms.addRoom(room);            
        }

        public static void addNewRoomCourse(ChatRoomList list, String roomName, String roomDescr,int id)
        {
                
                ChatRoom room = new ChatRoom(roomName, roomDescr,id);
                
                rooms.addRoomCourse(room);              
        }
        /** Called when servlet is being destroyed */

        public void destroy()
        {
                System.err.println("Destroying all rooms");
        }
}